package com.mf.ergate.web.common.utils;

import com.mf.ergate.web.common.exception.BusinessException;
import com.mf.ergate.web.security.LoginContext;
import com.mf.framework.common.model.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ReflectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.LinkedHashMap;

/**
 * 熙康参数工具类
 * User: zhaoming
 * Date: 2018-10-12
 * To change this template use File | Settings | File Templates.
 **/
public class MfParamUtils {

    /**
     * 构建insert方法的实体对象
     */
    public static  <E> E buildInsertEntity(E t){
        try {
            //创建人ID
            Field createUserNo = ReflectionUtils.findField(t.getClass(), "createUserNo");
            createUserNo.setAccessible(true);
            createUserNo.set(t, LoginContext.getLoginUser().getUserNo());
            //创建人名称
            Field createUserName = ReflectionUtils.findField(t.getClass(), "createUserName");
            createUserName.setAccessible(true);
            createUserName.set(t, LoginContext.getLoginUser().getUserName());
            //创建时间
            Field createTime = ReflectionUtils.findField(t.getClass(), "createTime");
            createTime.setAccessible(true);
            createTime.set(t, new Date());
            //修改人ID
            Field updateUserNo = ReflectionUtils.findField(t.getClass(), "updateUserNo");
            updateUserNo.setAccessible(true);
            updateUserNo.set(t, LoginContext.getLoginUser().getUserNo());
            //修改人名称
            Field updateUserName = ReflectionUtils.findField(t.getClass(), "updateUserName");
            updateUserName.setAccessible(true);
            updateUserName.set(t, LoginContext.getLoginUser().getUserName());
            //修改时间
            Field updateTime = ReflectionUtils.findField(t.getClass(), "updateTime");
            updateTime.setAccessible(true);
            updateTime.set(t, new Date());
        } catch (Exception e) {
            throw new BusinessException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e);
        }
        return t;
    }

    /**
     * 构建update方法的实体对象
     */
    public static  <E> E buildUpdateEntity(E t){
        try {
            //修改人ID
            Field updateUserNo = ReflectionUtils.findField(t.getClass(), "updateUserNo");
            updateUserNo.setAccessible(true);
            updateUserNo.set(t, LoginContext.getLoginUser().getUserNo());
            //修改人名称
            Field updateUserName = ReflectionUtils.findField(t.getClass(), "updateUserName");
            updateUserName.setAccessible(true);
            updateUserName.set(t, LoginContext.getLoginUser().getUserName());
            //修改时间
            Field updateTime = ReflectionUtils.findField(t.getClass(), "updateTime");
            updateTime.setAccessible(true);
            updateTime.set(t, new Date());
        } catch (Exception e) {
            throw new BusinessException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e);
        }
        return t;
    }

    /**
     * 构建分页对象
     * @param pageNum 当前页码
     * @param pageSize 每页显示条数
     * @param sortName 排序字段
     * @param sortOrder 排序方式
     * @return 分页对象
     */
    public static Page buildPage(int pageNum, int pageSize, String sortName, String sortOrder){
        Page page = new Page(pageNum, pageSize);
        if(StringUtils.isNotBlank(sortName) && StringUtils.isNotBlank(sortOrder)){
            LinkedHashMap<String, String> sortItemMap = new LinkedHashMap<>();
            sortItemMap.put(sortName, sortOrder);
            page.setSortItemMap(sortItemMap);
        }
        return page;
    }
}
